package main

import (
	"strings"
	"unicode"
)

// 自己写的
func reversePrefix(word string, ch byte) string {
	// 1. 找到当前出现过的字符ch
	idx := 0
	for i, c := range word {
		if byte(c) == ch {
			idx = i
			break
		}
		if i == len(word)-1 {
			return word
		}
	}
	// 2. 找出两段字符串
	endS := word[idx+1:]
	sList := []byte(word[:idx+1])
	for i, j := 0, len(sList)-1; i < j; i, j = i+1, j-1 {
		sList[i], sList[j] = sList[j], sList[i]
	}
	// 3.返回结果
	return string(sList) + endS
}

// 官方题解
func reversePrefix(word string, ch byte) string {
	// 调用方法
	right := strings.IndexByte(word, ch)
	if right < 0 {
		return word
	}
	s := []byte(word)
	for left := 0; left < right; left++ {
		s[left], s[right] = s[right], s[left]
		right--
	}
	return string(s)
}
